package arko.dzb.biz.app.plugins;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import arko.dzb.biz.app.oc.R;

/**
 * 前端js 连接java 执行拍照 选择图库等
 * 
 * @description
 * @version 1.0
 * @author 李梦龙
 * @date 2016-5-21 下午2:52:24
 */
public class TakePhoto extends CordovaPlugin {

	private static TakePhoto instance;

	// 拍照成功，读取相册成功，裁减成功
	private final int ALBUM_OK = 1, CAMERA_OK = 2, CUT_OK = 3;
	private ImageView showIv;
	private File file;

	public TakePhoto() {
		instance = this;
	}

	@Override
	public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);
	}

	CallbackContext callbackContext;
	Uri photoUri,uri;

	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
		this.callbackContext = callbackContext;

		if ("takePhoto".equals(action)) {
			Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			// 下面这句指定调用相机拍照后的照片存储的路径
			SimpleDateFormat timeStampFormat = new SimpleDateFormat(
					"yyyy_MM_dd_HH_mm_ss");
			String filename = timeStampFormat.format(new Date());
			ContentValues values = new ContentValues();
			values.put(Media.TITLE, filename);
			photoUri = this.cordova
					.getActivity()
					.getContentResolver()
					.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
							values);

			cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

			// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
			// Uri.fromFile(file));
			this.cordova.startActivityForResult(this, cameraIntent, CAMERA_OK);
			return true;
		}

		return false;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		callbackContext.success("成功：" );
		switch (requestCode) {
		// 如果是直接从相册获取
		case ALBUM_OK:
			// 从相册中获取到图片了，才执行裁剪动作
			callbackContext.success("成功：" + data.getDataString());
			if (data != null) {
				callbackContext.success("成功：" + data.getDataString());
				return;
				// clipPhoto(data.getData());
			}
			break;
		// 如果是调用相机拍照时
		case CAMERA_OK:
			// 当拍照到照片时进行裁减，否则不执行操作
			callbackContext.success("拍照成功：");
			if (data != null && data.getData() != null) {
				uri = data.getData();
			}
			if (uri == null) {
				if (photoUri != null) {
					uri = photoUri;
				}
			}
			callbackContext.success("拍照成功" + data + " uri:"+uri);
			// if (file.exists()) {
			// callbackContext.success("图片存在" + data);
			// clipPhoto(Uri.fromFile(file));// 开始裁减图片
			// }
			break;
		// 取得裁剪后的图片，这里将其设置到imageview中
		case CUT_OK:
			/**
			 * 非空判断大家一定要验证，如果不验证的话， 在剪裁之后如果发现不满意， 要重新裁剪，丢弃 当前功能时，会报NullException
			 */
			if (data != null) {
				callbackContext.success("测试成功" + data);
				// setPicToView(data);
			}
			break;
		default:
			break;

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 裁剪图片方法实现
	 * 
	 * @param uri
	 */
	public void clipPhoto(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop = true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例，这里设置的是正方形（长宽比为1:1）
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 200);
		intent.putExtra("outputY", 200);
		intent.putExtra("return-data", true);
		this.cordova.startActivityForResult(this, intent, CUT_OK);
	}

}